﻿<?php
require_once("../config.php");
require_once(_SRC_."/dao/PageDao.php");

require_once _SRC_."/domain/PageTemplate.php";
require_once(_SRC_."/dao/PageTemplateDao.php");
?>
<script type="text/javascript">
function popupDialog(pageId){
	
}
</script>
<?php
	$pageDao = new PageDao();
	$pages = $pageDao->getPages();
	
	$pageTemplateDao = new PageTemplateDao();
	$pageTemplates = $pageTemplateDao->getPageTemplates();
	
	if(isset($_GET["action"]) && $_GET["action"] == "addPage"){
		$page = new Page();
		$page->name = $_POST["name"];
		$page->slug = $_POST["slug"];
		$page->templateId = $_POST["templateId"];
		$pageDao->addPage($page);
		header('Location: index.php?type=page');
	}
?>

	<form name="form" method="post" action="index.php?type=page&action=addPage">
	<table border=1 id="pageTable">
			<tr>
				<td>编号</td><td>名称</td><td>别称</td><td>模板</td><td>操作</td>
			</tr>
			<?php
			foreach ($pages as $page){
				$id = $page->id;
				echo '<tr class="dataRow">';
				echo '<td>'.$id.'</td>';
				echo '<td>'.$page->name.'</td>';
				echo '<td>'.$page->slug.'</td>';
				$pageTemplate = $pageTemplateDao->getPageTemplate($page->templateId);
				echo '<td>'.$pageTemplate->name.'</td>';
				echo '<td>&nbsp;<A href="index.php?type=content&pageId='.$id.'">编辑内容</A>&nbsp;</td>';
				echo '</tr>';		
			}
			?>
	</table>
	<p>
		<A href="#" id="addPageLink">添加页面</A>
	</p>
	<div class="buttonBar"><input type="submit" id="saveButton" value="保存"></div>
	</form>
	
	
	<script type="text/javascript">
		$("#addPageLink").click(function(){
			var templates = <?php echo(json_encode($pageTemplates)) ?>;
			var idstr= $(".dataRow").last().find(":first-child").html();
			var id = idstr?parseInt(idstr)+1:1;
		
			var html = '<tr class="dataRow"><td>'+id+'</td>'
			+'<td><input type="text" name="name"/></td>'
			+'<td><input type="text" name="slug"/></td>'
			+'<td><select name="templateId">';			
			
			for(var i=0; i<templates.length; i++){
				var template = templates[i];
				html += '<option value='+template.id+'>'+template.name+'</option>';
			}
			
			html += '</select></td><td></td></tr>';
			
			$("#pageTable").append(html);
			$("#addPageLink").hide();
	});	
	</script>